﻿//---------------------------------------------------
// 将指定的number值添加到日期
// interval:s（秒）m（分）h（小时）d（天）w（周） q（季度） M（月）y (年)
// number:数值
//---------------------------------------------------
Date.prototype.Add = function (interval, number) {
    switch (interval) {
        case 's': return new Date(Date.parse(this) + (1000 * number));
        case 'm': return new Date(Date.parse(this) + (60000 * number));
        case 'h': return new Date(Date.parse(this) + (3600000 * number));
        case 'd': return new Date(Date.parse(this) + (86400000 * number));
        case 'w': return new Date(Date.parse(this) + ((86400000 * 7) * number));
        case 'M': return new Date(this.getFullYear(), (this.getMonth()) + number, this.getDate());
        case 'q': return new Date(this.getFullYear(), (this.getMonth()) + number * 3, this.getDate());
        case 'y': return new Date((this.getFullYear() + number), this.getMonth(), this.getDate());
    }
}
Date.prototype.AddSecond = function (number) { return this.Add("s", number); }
Date.prototype.AddMinute = function (number) { return this.Add("m", number); }
Date.prototype.AddHour = function (number) { return this.Add("h", number); }
Date.prototype.AddDay = function (number) { return this.Add("d", number); }
Date.prototype.AddWeek = function (number) { return this.Add("w", number); }
Date.prototype.AddMonth = function (number) { return this.Add("M", number); }
Date.prototype.AddQuarter = function (number) { return this.Add("q", number); }
Date.prototype.AddYear = function (number) { return this.Add("y", number); }
//---------------------------------------------------   
// 日期格式化   
// 格式 YYYY/yyyy/YY/yy 表示年份   
// MM/M 月份   
// W/w 星期   
// dd/DD/d/D 日期   
// hh/HH/h/H 时间   
// mm/m 分钟   
// ss/SS/s/S 秒   
//---------------------------------------------------   
Date.prototype.Format = function (formatStr) {
    var Week = ['日', '一', '二', '三', '四', '五', '六'];
    formatStr = formatStr.replace(/yyyy|YYYY/, this.getFullYear());
    formatStr = formatStr.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
    formatStr = formatStr.replace(/MM/, this.getMonth() + 1 > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
    formatStr = formatStr.replace(/M/g, (this.getMonth() + 1));
    formatStr = formatStr.replace(/w|W/g, Week[this.getDay()]);
    formatStr = formatStr.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    formatStr = formatStr.replace(/d|D/g, this.getDate());
    formatStr = formatStr.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    formatStr = formatStr.replace(/h|H/g, this.getHours());
    formatStr = formatStr.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    formatStr = formatStr.replace(/m/g, this.getMinutes());
    formatStr = formatStr.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    formatStr = formatStr.replace(/s|S/g, this.getSeconds());
    return formatStr;
}
//---------------------------------------------------
// 获取工作天数
// duty: 假期类型0->标准日期  1->单休  2->双休
// startDate: 开始日期
// endDate:   结束日期
//---------------------------------------------------
function getWorkDays(duty, startDate, endDate) {
    if (startDate == "" || endDate == "") return;
    startDate = new Date(startDate.replace(/-/g, "/"));
    endDate = new Date(endDate.replace(/-/g, "/"));
    var i = 0;
    while (startDate <= endDate) {
        switch (duty) {
            case "0": i++; break;
            case "1": if (startDate.getDay() != 6) i++; break;
            case "2": if (startDate.getDay() != 6 && startDate.getDay() != 0) i++; break;
        }
        startDate = startDate.AddDay(1);
    }
    return i;
}
//---------------------------------------------------
// 获取工期的开始日期与结束日期
// duty: 假期类型0->标准日期  1->单休  2->双休
// days: 天数
//---------------------------------------------------
function getWorkDate(duty, days, startDate) {
    if (isNaN(days)) return null;
    days = parseInt(days) - 1;
    var endDate = new Date();
    if (startDate && startDate != "")
        endDate = new Date(startDate.replace(/-/g, "/"));
    else
        startDate = new Date().Format("yyyy-MM-dd");
    var i = 0;
    while (i < days) {
        switch (duty) {
            case "0": i++; break;
            case "1": if (endDate.getDay() != 6) i++; break;
            case "2": if (endDate.getDay() != 6 && endDate.getDay() != 0) i++; break;
        }
        endDate = endDate.AddDay(1);
    }
    return { "startDate": startDate, "endDate": endDate.Format("yyyy-MM-dd") };
}